package com.cls.business.controller;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.business.entity.DeclareTopic;
import com.cls.business.service.IDeclareTopicService;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.enums.DictEnum;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.cls.system.service.ISysDictService;
import com.github.pagehelper.PageInfo;
import com.cls.business.entity.AllotmentCapital;
import com.cls.business.service.IAllotmentCapitalService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 拨付资金表 Controller
 *
 * @author slx
 * @date 2020-11-20 09:33:12
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class AllotmentCapitalController extends BaseController {

    private final IAllotmentCapitalService allotmentCapitalService;
    private final IDeclareTopicService declareTopicService;
    private final ISysDictService sysDictService;
    /**
    * 拨付资金表列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/allotmentCapital")
    public String allotmentCapitalIndex(Model model){

        return MyUtil.view("business/allotmentCapital/allotmentCapital");
    }

    /**
    * 拨付资金表新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/allotmentCapital/allotmentCapitalAdd")
    public String allotmentCapitalAdd(Model model){
        //初始化所有课题list
        DeclareTopic declareTopic =new DeclareTopic();
        declareTopic.setStatusTypeCode(DictEnum.ITEM_PASSED.getCode());
        //所有满足条件的课题
        List<DeclareTopic> declareTopicList = declareTopicService.applyCapitalDeclareTopics();
        model.addAttribute("declareTopicList",declareTopicList);
        return MyUtil.view("business/allotmentCapital/allotmentCapitalAdd");
    }

    /**
    * 拨付资金表修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/allotmentCapital/allotmentCapitalUpdate/{allotmentCapitalId}")
    public String allotmentCapitalUpdate(@PathVariable Long allotmentCapitalId, Model model){
        AllotmentCapital allotmentCapital = allotmentCapitalService.getById(allotmentCapitalId);
        model.addAttribute("allotmentCapital",allotmentCapital);
        //初始化所有课题list
        List<DeclareTopic> declareTopicList = declareTopicService.applyCapitalDeclareTopics();
        model.addAttribute("declareTopicList",declareTopicList);
        return MyUtil.view("business/allotmentCapital/allotmentCapitalUpdate");
    }



    /**
    * 拨付资金表详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/allotmentCapital/allotmentCapitalDetail/{allotmentCapitalId}")
    public String allotmentCapitalDetail(@PathVariable Long allotmentCapitalId, Model model){
        AllotmentCapital allotmentCapital = allotmentCapitalService.selectDetailById(allotmentCapitalId);
        model.addAttribute("allotmentCapital",allotmentCapital);
        return MyUtil.view("business/allotmentCapital/allotmentCapitalDetail");
    }

    @GetMapping("allotmentCapital")
    @ResponseBody
    @RequiresPermissions("allotmentCapital:list")
    public ResponseVO getAllAllotmentCapitals(AllotmentCapital allotmentCapital) {
        return ResponseVO.success(allotmentCapitalService.findAllotmentCapitals(allotmentCapital));
    }
    //根据课题id查询所有金额列表lists
    @GetMapping("allotmentCapital/getListByDeclareId/{declareId}")
    @ResponseBody
    public ResponseVO getAllAllotmentCapitalList(QueryRequest request,@PathVariable Long declareId) {
        return ResponseVO.success(allotmentCapitalService.findAllotmentCapitalsByDeclareId(request,declareId));
    }
    @GetMapping("allotmentCapital/list")
    @ResponseBody
    @RequiresPermissions("allotmentCapital:list")
    public ResponseVO allotmentCapitalList(QueryRequest request, AllotmentCapital allotmentCapital) {
    /*    PageInfo<AllotmentCapital> pageInfo =  this.allotmentCapitalService.findAllotmentCapitals(request, allotmentCapital);*/
        PageInfo<AllotmentCapital> pageInfo =  this.allotmentCapitalService.findAllotmentCapitalsPage(request, allotmentCapital);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增AllotmentCapital", exceptionMessage = "新增AllotmentCapital失败")
    @PostMapping("allotmentCapital")
    @ResponseBody
    @RequiresPermissions("allotmentCapital:add")
    public ResponseVO addAllotmentCapital(@Valid AllotmentCapital allotmentCapital) {
        System.out.println("allotmentCapital");
        this.allotmentCapitalService.createAllotmentCapital(allotmentCapital);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除AllotmentCapital", exceptionMessage = "删除AllotmentCapital失败")
    @PostMapping("allotmentCapital/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("allotmentCapital:delete")
    public ResponseVO deleteAllotmentCapitals(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.allotmentCapitalService.deleteAllotmentCapitals(idArry);
        return ResponseVO.success();
    }
    @ControllerEndpoint(operation = "发布", exceptionMessage = "发布失败")
    @PostMapping("allotmentCapital/pubish/{allotmentCapitalId}")
    @ResponseBody
    @RequiresPermissions("allotmentCapital:update")
    public ResponseVO pubishExpert(@PathVariable Long allotmentCapitalId,Boolean isPublish) {
        AllotmentCapital allotmentCapital=new AllotmentCapital();
        allotmentCapital.setAllotmentCapitalId(allotmentCapitalId);
        allotmentCapital.setIsPublish(isPublish);
        this.allotmentCapitalService.updateAllotmentCapital(allotmentCapital);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "删除单个AllotmentCapital", exceptionMessage = "删除AllotmentCapital失败")
    @PostMapping("allotmentCapital/delete/{allotmentCapitalId}")
    @ResponseBody
    @RequiresPermissions("allotmentCapital:delete")
    public ResponseVO deleteAllotmentCapital(@PathVariable Long allotmentCapitalId) {
        this.allotmentCapitalService.removeById(allotmentCapitalId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改AllotmentCapital", exceptionMessage = "修改AllotmentCapital失败")
    @PostMapping("allotmentCapital/update")
    @ResponseBody
    @RequiresPermissions("allotmentCapital:update")
    public ResponseVO updateAllotmentCapital(AllotmentCapital allotmentCapital) {
        this.allotmentCapitalService.updateAllotmentCapital(allotmentCapital);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改AllotmentCapital", exceptionMessage = "导出Excel失败")
    @PostMapping("allotmentCapital/excel")
    @ResponseBody
    @RequiresPermissions("allotmentCapital:export")
    public void export(QueryRequest queryRequest, AllotmentCapital allotmentCapital, HttpServletResponse response) {
        List<AllotmentCapital> allotmentCapitals = this.allotmentCapitalService.findAllotmentCapitals(queryRequest, allotmentCapital).getList();
        ExcelKit.$Export(AllotmentCapital.class, response).downXlsx(allotmentCapitals, false);
    }
}
